#include "ipc.h"
#include "mysql.h"
#include "sendmail.h"

#define BUFFER_POOL_SIZE 1024
#define BUFFER_SIZE 4096

static uv_udp_t udp_handle;
static char buffer_pool[BUFFER_POOL_SIZE][BUFFER_SIZE];
static int buffer_pool_index = 0;

// ipc回调函数在以下位置添加：
static void cpu_load_cb(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags) {
    if (nread > 0) {
        char value[BUFFER_SIZE];
        snprintf(value, sizeof(value), "%.*s", (int)nread - 1, buf->base + 1);
        store_cpu_load(value);

        // 阈值判断
        double cpu_load = atof(value);
        if (cpu_load > 1.0) { // 示例阈值
            send_alert_email("CPU负载过高");
        }
    }
}

static void memory_user_cb(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags) {
    if (nread > 0) {
        char value[BUFFER_SIZE];
        snprintf(value, sizeof(value), "%.*s", (int)nread - 1, buf->base + 1);
        store_memory_user(value);

        // 阈值判断
        double memory_usage = atof(value);
        if (memory_usage > 50.0) { // 示例阈值
            send_alert_email("内存利用率过高");
        }
    }
}

static void disk_usage_cb(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags) {
    if (nread > 0) {
        char value[BUFFER_SIZE];
        snprintf(value, sizeof(value), "%.*s", (int)nread - 1, buf->base + 1);
        store_disk_usage(value);

        // 阈值判断
        double disk_usage = atof(value);
        if (disk_usage > 90.0) { // 示例阈值
            send_alert_email("磁盘使用率过高");
        }
    }
}

static void system_uptime_cb(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags) {
    if (nread > 0) {
        char value[BUFFER_SIZE];
        snprintf(value, sizeof(value), "%.*s", (int)nread - 1, buf->base + 1);
        store_system_uptime(value);

        // 阈值判断
        double uptime = atof(value);
        if (uptime > 86400.0) { // 示例阈值：1天
            send_alert_email("系统运行时间过长");
        }
    }
}

// IPC注册列表结构体
typedef struct ipc_reg_t {
    int msg_id;              // 消息ID
    uv_udp_recv_cb func;     // 回调函数
} ipc_reg_t;

// IPC注册列表
static ipc_reg_t s_serv_ipc_list[] = {
    //ipc回调函数设置在以下位置添加：
    {1, cpu_load_cb},       // CPU负载
    {2, memory_user_cb},    // 内存利用率
    {3, disk_usage_cb},     // 磁盘使用率
    {4, system_uptime_cb},  // 系统运行时间
    {0, NULL} // 结束标志
};

void on_alloc_buffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
    buf->base = buffer_pool[buffer_pool_index];
    buf->len = BUFFER_SIZE;
    buffer_pool_index = (buffer_pool_index + 1) % BUFFER_POOL_SIZE;
}

void on_read(uv_udp_t* req, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags) {
    if (nread > 0) {
        // 消息ID在数据的第一个字节
        int msg_id = buf->base[0];
        ipc_reg_t* ipc_t = s_serv_ipc_list;
        for (; ipc_t->func; ipc_t++) {
            if (ipc_t->msg_id == msg_id) {
                ipc_t->func(req, nread, buf, addr, flags);
                break;
            }
        }
    }
    // free(buf->base);
}

int init_ipc() {
    uv_loop_t* loop = uv_default_loop();
    if (uv_udp_init(loop, &udp_handle) != 0) {
        return -1;
    }

    struct sockaddr_in recv_addr;
    if (uv_ip4_addr("0.0.0.0", 12345, &recv_addr) != 0) {
        return -1;
    }
    if (uv_udp_bind(&udp_handle, (const struct sockaddr*)&recv_addr, UV_UDP_REUSEADDR) != 0) {
        return -1;
    }
    if (uv_udp_recv_start(&udp_handle, on_alloc_buffer, on_read) != 0) {
        return -1;
    }

    return 0;
}

void uninit_ipc() {
    uv_udp_recv_stop(&udp_handle);
}